//
//  HPScrollAndPageView.m
//  groupProject
//
//  Created by mac on 15/12/21.
//  Copyright © 2015年 mac. All rights reserved.
//

#import "HPScrollAndPageView.h"
#import "Config.h"
@implementation HPScrollAndPageView {
    UIView *_firstView;
    UIView *_middleView;
    UIView *_lastView;
    CGFloat _viewWidth;
    CGFloat _viewHeight;
    NSTimer *_autoScrollTimer;
    UITapGestureRecognizer *_tap;
}

- (id) initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        _viewWidth = frame.size.width;
        _viewHeight = frame.size.height;
        
        //添加scollview
        _scrollView = [[UIScrollView alloc] initWithFrame:frame];
        _scrollView.delegate = self;
        _scrollView.contentSize = CGSizeMake(SCREEN_WIDTH * 3, SCROLL_HEIGHT);
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.pagingEnabled  = YES;
        _scrollView.bounces = NO;
        [self addSubview:_scrollView];
        
        //添加pagecontrol
        _pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(SCREEN_WIDTH/2.0 - SCREEN_WIDTH*0.2, SCROLL_HEIGHT * 0.8, SCREEN_WIDTH * 0.4, SCROLL_HEIGHT *0.2)];
        _pageControl.numberOfPages = SCROLLPAGE;
        _pageControl.currentPageIndicatorTintColor = [UIColor redColor];
        _pageControl.pageIndicatorTintColor = [UIColor whiteColor];
        [self addSubview:_pageControl];
        
        //scroll 添加手势
        _tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
        _tap.numberOfTapsRequired = 1;
        _tap.numberOfTouchesRequired = 1;
        [_scrollView addGestureRecognizer:_tap];
        
        _autoScrollTimer = [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(autoShowNextImage) userInfo:nil repeats:YES];
    }
    
    
    return self;
}


#pragma mark 设置imageViewArray
-(void)setImageViewArray:(NSMutableArray *)imageViewArray
{
    if (imageViewArray) {
        
        _imageViewArray = imageViewArray;
        _currentPage = 0; //默认为第0页
        _pageControl.numberOfPages = _imageViewArray.count;
    }
    
    [self reloadData];
}

- (void) reloadData {
    [_firstView removeFromSuperview];
    [_middleView removeFromSuperview];
    [_lastView removeFromSuperview];
    
    //从数组中取到对应的图片view加到已定义的三个view中
    if (_currentPage==0) {
        _firstView = [_imageViewArray lastObject];
        _middleView = [_imageViewArray objectAtIndex:_currentPage];
        _lastView = [_imageViewArray objectAtIndex:_currentPage+1];
    }
    else if (_currentPage == _imageViewArray.count-1)
    {
        _firstView = [_imageViewArray objectAtIndex:_currentPage-1];
        _middleView = [_imageViewArray objectAtIndex:_currentPage];
        _lastView = [_imageViewArray firstObject];
    }
    else
    {
        _firstView = [_imageViewArray objectAtIndex:_currentPage-1];
        _middleView = [_imageViewArray objectAtIndex:_currentPage];
        _lastView = [_imageViewArray objectAtIndex:_currentPage+1];
    }
    
    //设置三个view的frame，加到scrollview上
    _firstView.frame = CGRectMake(0, 0, _viewWidth, _viewHeight);
    _middleView.frame = CGRectMake(_viewWidth, 0, _viewWidth, _viewHeight);
    _lastView.frame = CGRectMake(_viewWidth*2, 0, _viewWidth, _viewHeight);
    [_scrollView addSubview:_firstView];
    [_scrollView addSubview:_middleView];
    [_scrollView addSubview:_lastView];
    
    //设置当前的分页
    _pageControl.currentPage = _currentPage;
    
    //显示中间页
    _scrollView.contentOffset = CGPointMake(_viewWidth, 0);
}

#pragma mark scrollvie停止滑动
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    //手动滑动时候暂停自动替换
    [_autoScrollTimer invalidate];
    _autoScrollTimer = nil;
    _autoScrollTimer = [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(autoShowNextImage) userInfo:nil repeats:YES];
    
    //得到当前页数
    float x = _scrollView.contentOffset.x;
    
    //往前翻
    if (x<=0) {
        if (_currentPage-1<0) {
            _currentPage = _imageViewArray.count-1;
        }else{
            _currentPage --;
        }
    }
    
    //往后翻
    if (x>=_viewWidth*2) {
        if (_currentPage==_imageViewArray.count-1) {
            _currentPage = 0;
        }else{
            _currentPage ++;
        }
    }
    
    [self reloadData];
}

#pragma mark 自动滚动
-(void)shouldAutoShow:(BOOL)shouldStart
{
    if (shouldStart)  //开启自动翻页
    {
        if (!_autoScrollTimer) {
            _autoScrollTimer = [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(autoShowNextImage) userInfo:nil repeats:YES];
        }
    }
    else   //关闭自动翻页
    {
        if (_autoScrollTimer.isValid) {
            [_autoScrollTimer invalidate];
            _autoScrollTimer = nil;
        }
    }
}

#pragma mark 展示下一页
-(void)autoShowNextImage
{
    if (_currentPage == _imageViewArray.count-1) {
        _currentPage = 0;
    }else{
        _currentPage ++;
    }
    
    [self reloadData];
}


#pragma  mark - tap gesturerecognize
- (void) handleTap:(UITapGestureRecognizer *)sender {
    
}


@end
